package com.example.day12;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;

public class MyAsyncTask2 extends AsyncTask<String,Integer,String> {
    Context context;
    TextView text;
    ProgressBar pro;
    public MyAsyncTask2(Context context, TextView text, ProgressBar pro){
        this.context = context;
        this.text=text;
        this.pro = pro;
    }

    @Override
    protected String doInBackground(String... str) {
        String ret=null;
        String fileName = str[0];
        AssetManager assetM = context.getAssets();

        try {
            InputStream in = assetM.open(fileName);
            Log.e("长度",""+in.available()+"%%"+in.available()/100);
            pro.setMax(in.available());
            byte[] data = new byte[1024];
            int len=0;
            StringBuilder sb=new StringBuilder();
            int a=0;
            int length = in.available();
            while((len=in.read(data))!=-1){

                String s = new String(data, 0, len);
//                Log.e("####len",len+"");
                a+=len;
                sb.append(s);
                Log.e("####int",a/(in.available()/100)+"");
                publishProgress(a);
//                publishProgress((int)a/(length/100));
            }

            //sb就是文件拼接的全部内容

            in.read(data);
            ret = new String(data, 0, data.length);

//            Log.e("########", ret);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Log.e("####","事前");
        pro.setVisibility(View.VISIBLE);
        pro.setMax(100);
    }
    @Override
    protected void onPostExecute(String str){
        super.onPostExecute(str);
        Log.e("####","事后");
        pro.setVisibility(View.GONE);
        text.setText(str);
    }
    @Override
    public void onProgressUpdate(Integer... values){

        super.onProgressUpdate(values);
        pro.setProgress(values[0]);
    }
}
